<?php
require_once('common.php');
//************************执行代码开始*************************

if($_GET['do']=='del'){
	$kid=is_numeric($_GET['kid'])?$_GET['kid']:'0';
	if($kid && $row=$db->get_row("select * from {$prefix}kms where kid='{$kid}' limit 1")){
		if($db->query("delete from {$prefix}kms where kid='{$kid}' and daili='{$userrow[uid]}'")){
			echo"<script language='javascript'>alert('删除卡密成功！');</script>";
		}else{
			echo"<script language='javascript'>alert('删除卡密失败！');</script>";
		}
	}else{
		echo"<script language='javascript'>alert('要删除的卡密不存在！');</script>";
	}
}
if($_GET['do']=='dell'){
	$db->query("delete from {$prefix}kms where isuse='1'");
	echo "<script language='javascript'>alert('删除成功！');history.go(-1);</script>";
}
if($_GET['do']=='delll'){
	$db->query("delete from {$prefix}kms where 1");
	echo "<script language='javascript'>alert('清空成功！');history.go(-1);</script>";
}
if($_POST['do']=='add'){
	$num=is_numeric($_POST['num'])?$_POST['num']:'1';
	$rmb=is_numeric($_POST['rmb'])?$_POST['rmb']:'1';
	if($num>20) exit("<script language='javascript'>alert('一次性最多生成20个！');history.go(-1);</script>");
	if($rmb>100) exit("<script language='javascript'>alert('卡密最大面额为100元！');history.go(-1);</script>");
	$now = date("Y-m-d H:i:s");
	for($i=0;$i<$num;$i++){
		$km=get_sz();
		$nrmb=$userrow['rmb']-$rmb;
		
		if($db->query("insert into  {$prefix}kms (kind,daili,km,ms,isuse,uid,addtime) values (0,'$userrow[uid]','$km','$rmb',0,0,'$now')")){
		 $kmmsg.="<li class='list-group-item'>{$km}</li>";
		}
		
	}
}



$p=is_numeric($_GET['p'])?$_GET['p']:'1';
$pp=$p+8;
$pagesize=10;
$start=($p-1)*$pagesize;
$pages=ceil(get_count('kms',"daili='$userrow[uid]'",'kid')/$pagesize);
if(!$pages) $pages=1;
if($pp>$pages) $pp=$pages;
if($p==1){
	$prev=1;
}else{
	$prev=$p-1;
}
if($p==$pages){
	$next=$p;
}else{
	$next=$p+1;
}
$rows=$db->get_results("select * from {$prefix}kms where daili='$userrow[uid]' order by kid desc limit $start,$pagesize");
//************************执行代码结束**************************
C('pageid','km');
C('webtitle','卡密管理');
include_once 'common.head.php';
?>
  <div class="wrapper wrapper-content animated fadeInRight">
		<div class="col-sm-12">
					<div class="panel panel-default">
						<div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>余额充值卡密生成 <small>生成充值卡</small></h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
<?php
if($_GET['do']!="km"){
?>
				<div class="ibox-content">
                        <div class="row">
						<div id="collapseshop" class="panel-body">
							<form action="?" class="form-horizontal" method="post">
								<input type="hidden" name="do" value="add">
								<div class="form-group">
									<label class="col-sm-3 control-label" for="field-2">当前余额</label>
									<div class="col-sm-9">
										<input class="form-control" type="text" placeholder="<?=$userrow[rmb]?>" readonly>
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label" for="field-2">生成个数</label>
									<div class="col-sm-9">
										<input class="form-control" name="num" type="text" value="1">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label" for="field-2">卡密余额</label>
									<div class="col-sm-9">
										<input class="form-control" name="rmb" type="text" value="1">
									</div>
								</div>
								
								<div class="hr-line-dashed"></div>
                            <div class="form-group">
								<div class="col-sm-12 col-sm-offset-8">
								<a class="btn btn-info" href="?do=km">查看全部卡密</a>
                                    <button class="btn btn-primary" type="submit">生成充值卡</button>
									<?php if($kmmsg){?>
                            <a data-toggle="modal" class="btn btn-danger" href="#modal-form">查看已生成卡密</a>
				<?php }?>
                                </div>
                            </div>
							</form>
						</div>
					</div>
				</div>
			
			</div></div>
	<div id="modal-form" class="modal fade" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <div class="row">
                        <div id="collapseshop" class="panel-body">
						<h3 class="m-t-none m-b">已生成卡密</h3>
						<ul>
							<?=$kmmsg?>
						</ul>
						<a data-toggle="modal" class="btn btn-white pull-right" href="#modal-form">返回</a>
						</div>
                    </div>
                </div>
            </div>
        </div>
	</div>
<?php
}else{
?>
<div class="ibox-content">
	<div class="table-responsive">
                          <table class="table table-striped">
				<thead>
				<tr>
					<th>#KID</th>
					<th>卡密</th>
					<th>余额</th>
					<th>生成时间</th>
					<th>是否使用</th>
					<th>使用时间</th>
					<th>操作</th>
				</tr>
				</thead>
				<tbody>
				<?php if($rows){foreach($rows as $km){?>
				<tr>
					<td><?=$km[kid]?></td>
					<td><?=$km[km]?></td>
					<td><?=$km[ms]?></td>
					<td><?=$km[addtime]?></td>
					<td><?php if($km[isuse]){echo'<font color="red">已使用</font>';}else{echo'<font color="green">未使用</font>';}?></td>
					<td><?=$km[usetime]?></td>
					<td><a href="?do=del&p=<?=$p?>&kid=<?=$km[kid]?>" class="btn btn-danger" onClick="if(!confirm('确认删除？')){return false;}">删除</a>&nbsp;</td>
				</tr>
				<?php }}?>
				</tbody>
				</table>
	</div>
	<div class="row" style="text-align:center;">
				<ul class="pagination pagination-lg">
					<li <?php if($p==1){echo'class="disabled"';}?>><a href="?p=1&do=km">首页</a></li>
					<li <?php if($prev==$p){echo'class="disabled"';}?>><a href="?p=<?=$prev?>&do=km">&laquo;</a></li>
					<?php for($i=$p;$i<=$pp;$i++){?>
					<li <?php if($i==$p){echo'class="active"';}?>><a href="?p=<?=$i?>&do=km"><?=$i?></a></li>
					<?php }?>
					<li <?php if($next==$p){echo'class="disabled"';}?>><a href="?p=<?=$next?>&do=km">&raquo;</a></li>
					<li <?php if($p==$pages){echo'class="disabled"';}?>><a href="?p=<?=$pages?>&do=km">末页</a></li>
				</ul>
	</div>
		
		</div></div></div>
<?php
}
?>
</div>
	</div>
	  <?php
include_once 'common.foot.php';
?>